package com.helios.core.util;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.google.gson.Gson;
import com.helios.core.bean.Bean;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Component
public class RedisUtil {
    
    private static final Logger logger = LogManager.getLogger(RedisUtil.class);
    
    private static final String REDIS_KEY      = "WEB_CMS_";
    
    private static final String REDIS_BEAN_KEY = "BEAN_";
    
    private static final String REDIS_MAP_KEY  = "MAP_";
    
    private static final String REDIS_LIST_KEY = "LIST_";
    
    private static ShardedJedisPool pool;
    
    @Resource  
    private ShardedJedisPool cachePool;  
    
    @PostConstruct  
    public void init(){  
        RedisUtil.pool = cachePool;  
    }  
    
    /************** 普通字符串  **************/
    public static String get(String key) {
        key = REDIS_KEY + key;
        logger.debug("获取缓存,key:" + key);
        try {
            ShardedJedis jedis = pool.getResource();
            return jedis.get(key);
        } catch (Exception e) {
            logger.warn("获取缓存失败,key:" + key);
            return null;
        }
    }
    
    public static String put(String key, String value) {
        key = REDIS_KEY + key;
        logger.debug("设置缓存,key:" + key + ",value:" + value);
        try {
            ShardedJedis jedis = pool.getResource();
            return jedis.set(key, value);
        } catch (Exception e) {
            logger.warn("设置缓存失败,key:" + key);
            return null;
        }
    }
    
    public static String put(String key, String value, int seconds) {
        key = REDIS_KEY + key;
        logger.debug("设置缓存,key:" + key + ",value:" + value + ",有效时间:" + seconds);
        try {
            ShardedJedis jedis = pool.getResource();
            return jedis.setex(key, seconds, value);
        } catch (Exception e) {
            logger.warn("设置缓存失败,key:" + key);
            return null;
        }
    }
    
    /************** 存储对象   **************/
    @SuppressWarnings("unchecked")
    public static <T extends Bean> T getObject(String key, Class<?> clazz) {
        return (T) new Gson().fromJson(get(key), clazz);
    }
    
    public static String putObject(String key, Object value) {
        return put(key, new Gson().toJson(value));
        
    }
    
    public static String putObject(String key, Object value, int seconds) {
        return put(key, new Gson().toJson(value), seconds);
    }
    
    /************** 扩展格式  **************/
    public static <T extends Bean> T getBean(String key, Class<? extends Bean> clazz) {
        return getObject(REDIS_BEAN_KEY + key, clazz);
    }
    
    public static String putBean(String key, Bean value) {
        return putObject(REDIS_BEAN_KEY + key, value);
    }
    
    public static String putBean(String key, Bean value, int seconds) {
        return putObject(REDIS_BEAN_KEY + key, value);
    }
    
    public static <T extends Bean> Map<String, T> getMap(String key) {
        return getObject(REDIS_MAP_KEY + key, Map.class);
    }
    
    public static <T extends Bean> String putMap(String key, Map<String, T> value) {
        return putObject(REDIS_MAP_KEY + key, value);
    }
    
    public static <T extends Bean> String putMap(String key, Map<String, T> value, int seconds) {
        return putObject(REDIS_MAP_KEY + key, value, seconds);
    }
    
    public static <T extends Bean> List<T> getList(String key) {
        return getObject(REDIS_LIST_KEY + key, Map.class);
    }
    
    public static <T extends Bean> String putList(String key, List<T> value) {
        return putObject(REDIS_LIST_KEY + key, value);
    }
    
    public static <T extends Bean> String putList(String key, List<T> value, int seconds) {
        return putObject(REDIS_LIST_KEY + key, value, seconds);
    }
    
    /************** 删除  **************/
    public static long del(String key) {
        return delCache(REDIS_KEY + key);
    }
    
    public static long delBean(String key) {
        return delCache(REDIS_BEAN_KEY + key);
    }
    
    public static long delMap(String key) {
        return delCache(REDIS_MAP_KEY + key);
    }
    
    public static long delList(String key) {
        return delCache(REDIS_LIST_KEY + key);
    }
    
    private static long delCache(String key) {
        logger.debug("删除缓存,key:" + key);
        try {
            ShardedJedis jedis = pool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            logger.warn("删除缓存失败,key:" + key);
            return 0;
        }
    }
}
